package auctionhaus
//Authors: Paul Kleczka, Adam Backstrom


import org.junit.Before
import org.junit.Test
import static org.junit.Assert.assertEquals
import grails.plugin.jms.JmsService
import grails.converters.JSON

class EmailServiceIntegrationTests {
    Customer seller     //the seller for the listing
    Customer bidderOne  //the first bidder for the listing

    Listing listing
    EmailService emailService = new EmailService()
    MailJob mailJob = new MailJob()

    @Before
    void setUp() {
        //seed database with test data
        seller = new Customer(email: "adam@adambackstrom.com",password: "123456", enabled: true)
        assert seller.validate()
        seller.save(flush: true)

        bidderOne = new Customer(email: "adambackstrom@gmail.com",password: "7891011", enabled: true)
        assert bidderOne.validate()
        bidderOne.save(flush: true)

        listing = new Listing(name: "listing1",
                endDateTime: new Date() + 1,
                startingBidPrice: 1.0f,
                seller: seller)
        assert listing.validate()
        listing.save(flush: true)
    }

    //HW 3  Requirement C-1
    //Test that a message is getting put on a queue
    //and getting pulled off by SendMailService
    //The message will cause an exception because of the bad TO address
    @Test
    void testSendWinnerEmail(){

        Bid firstBid = new Bid(amount: 1.5f,
                bidDateTime: new Date(),
                bidder: bidderOne)
        listing.addToBids(firstBid)
        listing.setWinner(bidderOne)
        listing.save(flush: true)
        assert firstBid.validate()


        Email email = new Email(to: "fake address",
                fromField: "auctionhowse@gmail.com",
                body: "body",
                subject: "Your Listing has completed",
                view: "/email/email",
                listingId: listing.id)

        try {
            emailService.sendEmail((email as JSON).toString())
        }
        catch (Exception e){
            assert e instanceof org.springframework.mail.MailParseException
        }
    }

    

}
